{extend name="../../common/view/admin_base"/}

{block name="main"}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-form">
                        <form method="post">
                            {volist name="fields" id="v"}
                            {$Render->render($v,$model)|raw}
                            {/volist}
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit="" lay-filter="commit">提交</button>
                                    <button class="layui-btn layui-btn-primary" lay-submit lay-filter="reset">重置</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="__static__/layui/extend/xm-select.js"></script>
<script>
    layui.use(['form','element','laydate','upload'],function(){
        var form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;

        $(".form-date").each(function(index,item){
            var id = $(item).attr('id');
            var val = $(item).val();
            var format = $(item).attr('format');
            var config = {};
            config.elem = '#'+id;
            config.format = format==undefined?'yyyy-MM-dd HH:mm:ss':format;
            config.type = config.format.indexOf('HH')>0?'datetime':'date';
            laydate.render(config);
        });


        $(".combotree").each(function(index,item){
            var id = $(item).attr('id');
            var val = $(item).attr('val');
            var name = $(item).attr('name');
            var idkey = $(item).attr('idkey');
            var namekey = $(item).attr('namekey');
            var pidkey = $(item).attr('pidkey');
            var url = $(item).attr('url');
            var root = $(item).attr('root');

            $.get(url,function(res){
                var data = res.data;
                var tree = $.getcombotreedata(data,root,idkey,namekey,pidkey);
                xmSelect.render({
                    el: "#"+id,
                    name:name,
                    radio: true,
                    initValue: val,
                    tree: {
                        show: true,
                        showFolderIcon: true,
                        showLine: true,
                        indent: 20
                    },
                    height: '200px',
                    data:tree
                })
            });
        });

        $.getcombotreedata = function(list,val,idkey,namekey,pidkey){
            if(list == undefined)
                return;
            var res = [];
            $.each(list,function(i,node){
                if (node[pidkey] == val) {
                    var newnode = {};
                    newnode['name'] = node[namekey];
                    newnode['value'] = node[idkey];
                    var children = $.getcombotreedata(list, node[idkey], idkey, namekey, pidkey);
                    if(children.length>0)
                        newnode['children'] = children;
                    res.push(newnode);
                }
            });
            return res;
        };

        $(".uploadfile").each(function(index,item){
            var id = $(item).attr('id');
            upload.render({
                elem: "#"+id
                ,url: "{:url('admin/ueditor/index')}?action=uploadfile"
                ,field:'upfile'
                ,accept: 'file' //普通文件
                ,done: function(res){
                    var data = res.data;
                    $("#"+id).parent().prev().find('input').val(data.url);
                }
            });
        });
        $(".uploadimg").each(function(index,item){
            var id = $(item).attr('id');
            var maxsize = $(item).attr('maxsize');
            var ext = $(item).attr('ext').replace(',','|');
            upload.render({
                elem: "#"+id
                ,url: "{:url('admin/ueditor/index')}?action=uploadimage"
                ,field:'upfile'
                ,size:maxsize
                ,exts:ext
                ,accept: 'images' //普通文件
                ,done: function(res){
                    var data = res.data;
                    $("#"+id).parent().prev().find('input').val(data.url);
                }
            });
        });
        form.on('submit(commit)', function(obj){
            $.post({
                url:document.URL
                ,type:'post'
                ,data: obj.field
                ,success:function(res){
                    if(res.code == 1){
                        layer.msg(res.msg, {
                            offset: '15px'
                            ,icon: 1
                            ,time: 1000
                        }, function(){
                            location.href = res.url;
                        });
                    }else{
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        });

    });
</script>
{/block}